<template>
  <el-card style="margin: 10px">
    <div class="search">
      <el-input
        placeholder="角色名称"
        size="small"
        style="width: 200px"
        v-model="roleName"
      ></el-input>
      <el-button
        type="primary"
        icon="el-icon-search"
        size="small"
        @click="inquireRolename"
        >查询</el-button
      >
      <el-button size="small" @click="emptyRolename">清空</el-button>
    </div>
    <el-button
      type="primary"
      size="samll"
      v-if="$has('btn.Role.add')"
      @click="addRole"
      >添加</el-button
    >
    <el-button
      type="danger"
      size="samll"
      @click="AllCheckedRemove"
      :disabled="!AllChecked.length >= 1"
      >批量删除</el-button
    >
    <el-table
      border
      style="margin-top: 20px"
      :data="rolePageList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"></el-table-column>
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="80px"
      ></el-table-column>
      <el-table-column label="角色名称">
        <template v-slot="{ row }">
          <div v-if="row.isEdit">
            <el-input
              size="mini"
              v-model="EditroleName"
              placeholder=""
              ref="showinput"
            ></el-input>
            <el-button
              type="warning"
              size="mini"
              icon="el-icon-refresh"
              @click="clearRole(row)"
              >取消输入</el-button
            >
          </div>
          <div v-else>{{ row.roleName }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            size="mini"
            type="info"
            icon="el-icon-warning"
            title="分配权限"
            v-if="$has('btn.Role.assgin')"
            @click="privileges(row)"
          ></el-button>
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-check"
            title="确定"
            @click="saveRole(row)"
          ></el-button>
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            title="修改角色"
            v-if="$has('btn.Role.update')"
            @click="EditRole(row)"
          ></el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            title="删除角色"
            v-if="$has('btn.Role.remove')"
            @click="removeRole(row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[5, 10, 20, 30]"
      background
      :page-size="limit"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
      style="margin-top: 20px"
    >
    </el-pagination>
  </el-card>
</template>

<script>
import {
  GetRolePageListApi,
  removeRoleListApi,
  removeRoleApi,
  saveRoleApi,
  updateRoleApi,
} from "@/api/acl/role";
import { Message } from "element-ui";
export default {
  name: "Role",
  mounted() {
    this.getRoleList();
  },
  data() {
    return {
      page: 1,
      limit: 5,
      total: 0,
      rolePageList: [],

      // 角色名称
      roleName: "",

      AllChecked: [],
      EditroleName: "",
    };
  },
  methods: {
    // 分配权限
    privileges(row) {
      this.$router.push({
        name: "RoleAuth",
        query: {
          id: row.id,
          roleName: row.roleName,
        },
      });
    },
    // 点击修改
    EditRole(row) {
      row.isEdit = true;
      this.EditroleName = row.roleName;

      this.$nextTick(() => {
        this.$refs.showinput.focus();
      });
    },
    // 确定修改
    async saveRole(row) {
      if (this.EditroleName) {
        await updateRoleApi(row.id, this.EditroleName);
      } else {
        await updateRoleApi(row.id, row.roleName);
      }
      row.isEdit = false;
      this.getRoleList();
      Message.success("更新角色成功");
      this.EditroleName = "";
    },
    // 点击取消编辑
    clearRole(row) {
      row.isEdit = false;
      Message.warning("取消角色修改");
    },
    // 添加角色
    addRole() {
      this.$prompt("请输入新的角色名称", "添加角色", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(async ({ value }) => {
          await saveRoleApi(value);
          this.$message({
            type: "success",
            message: "添加角色成功",
          });
          this.getRoleList();
        })
        .catch(() => {
          this.$message({
            type: "warning",
            message: "取消输入",
          });
        });
    },

    // 获取分页列表
    async getRoleList(rolename = "") {
      const result = await GetRolePageListApi(this.page, this.limit, rolename);

      const { total, items } = result.data;
      items.forEach((item) => {
        item.isEdit = false;
      });
      this.rolePageList = items;
      this.total = total;
    },
    // 改变每页条数
    handleSizeChange(val) {
      this.limit = val;
      this.getRoleList(this.roleName);
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.page = val;
      this.getRoleList(this.roleName);
    },

    // 点击删除
    async removeRole(id) {
      this.$confirm("删除选中的角色, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await removeRoleApi(id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getRoleList();
        })
        .catch((error) => {
          if (error) {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          }
        });
    },
    // 点击查询
    inquireRolename() {
      this.getRoleList(this.roleName);
    },
    // 点击清空
    emptyRolename() {
      this.roleName = "";
      this.getRoleList();
    },
    // 得到选中的事件
    handleSelectionChange(val) {
      this.AllChecked = val.map((item) => item.id);

      console.log(this.AllChecked);
    },

    // 点击批量删除
    async AllCheckedRemove() {
      this.$confirm("删除选中的用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await removeRoleListApi(this.AllChecked);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          // console.log(1);
          this.getRoleList();
        })
        .catch((error) => {
          if (error) {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          }
        });
    },
  },
};
</script>

<style lang="scss">
.search {
  display: flex;
  margin-bottom: 20px;

  button {
    margin-left: 10px;
  }
}
</style>
